package com.tencent.wuyanzu.whattoeat;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
import com.tencent.wuyanzu.whattoeat.Entity.Food;
import com.tencent.wuyanzu.whattoeat.util.ViewImageLoadURL;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by maiffany on 16/7/26.
 */
public class MealTimeActivity extends Activity {

    private TextView foodThurmUpTV = null;
    private TextView foodThurmDownTV = null;
    private TextView foodNameTV = null;
    private TextView foodPriceTV = null;
    private TextView foodRestaurantNameTV = null;
    private TextView foodLocationInfoTV = null;
    private TextView foodDescTV = null;
    private ImageView foodImageIV = null;


    private TextView timeShowTV=null;

    private Food selectFood=null;
    Handler handler = new Handler() {
        int timeleft=20;
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what)
            {
                case 1:
                    timeleft--;
                    timeShowTV.setText("timeleft:   "+timeleft);

                    if(timeleft==0)
                    {
                        timer.cancel();
                        Intent intent = new Intent(MealTimeActivity.this, EvaluateActivity.class);
                        intent.putExtra("foodDetail", selectFood);
                        startActivity(intent);
                        finish();
                    }
                    break;
                default:
                    break;
            }
        }
    };

    Timer timer = new Timer();

    TimerTask task = new TimerTask() {
        @Override
        public void run() {

            runOnUiThread(new Runnable() {      // UI thread
                @Override
                public void run() {
                    handler.sendEmptyMessage(1);
                }
            });
        }
    };
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_meal_time_detail);


        foodThurmDownTV = (TextView) findViewById(R.id.mealtime_thurm_down_number);
        foodThurmUpTV = (TextView) findViewById(R.id.mealtime_thurm_up_number);

        foodNameTV = (TextView) findViewById(R.id.mealtime_food_name);
        foodPriceTV = (TextView) findViewById(R.id.mealtime_food_price);
        foodRestaurantNameTV = (TextView) findViewById(R.id.mealtime_food_restaurant_name);
        foodLocationInfoTV = (TextView) findViewById(R.id.mealtime_food_location_info);
       // foodDescTV = (TextView) findViewById(R.id.mealtime_description);
        foodImageIV = (ImageView) findViewById(R.id.mealtime_food_imagine);

        timeShowTV=(TextView)findViewById(R.id.time_show);

        selectFood = (Food) getIntent().getExtras().get("foodDetail");
        foodNameTV.setText(selectFood.getName());
        //foodPriceTV.setText(selectFood.getPrice());
        foodRestaurantNameTV.setText(selectFood.getRestaurant());
        foodLocationInfoTV.setText(selectFood.getAddress());
      //  foodDescTV.setText(selectFood.getDescription());
        foodThurmUpTV.setText("" + selectFood.getUp());
        foodThurmDownTV.setText("" + selectFood.getDown());




        ViewImageLoadURL bitmapLoadURL = new ViewImageLoadURL();
        bitmapLoadURL.execute(selectFood.getUrl(), foodImageIV);

        timer.schedule(task,0,1000);
//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                Intent intent = new Intent(MealTimeActivity.this, EvaluateActivity.class);
//                intent.putExtra("foodDetail", selectFood);
//                startActivity(intent);
//                finish();
//            }
//        }, 3000);
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "MealTime Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app URL is correct.
                Uri.parse("android-app://com.tencent.wuyanzu.whattoeat/http/host/path")
        );
        AppIndex.AppIndexApi.start(client, viewAction);
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "MealTime Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app URL is correct.
                Uri.parse("android-app://com.tencent.wuyanzu.whattoeat/http/host/path")
        );
        AppIndex.AppIndexApi.end(client, viewAction);
        client.disconnect();
    }
}
